using UnityEngine;
using System;

public class DartScore : MonoBehaviour
{
    public TargetController targetController;
    void Start()
    {
       //targetController=FindObjectOfType<TargetController>();
    }
    void Update()
    {
        //targetController=FindObjectOfType<TargetController>();
    }
    public Action<int> onScoreCalculated;

    private bool hasScored = false;

     private void OnCollisionEnter(Collision collision)
    {
        // 关键调试日志
        Debug.Log($"飞镖碰撞到物体: {collision.gameObject.name}，标签: {collision.gameObject.tag}");
        
        if (hasScored) return;
        
        if (targetController == null)
        {
            Debug.LogError("靶盘引用为空！尝试自动查找...");
            targetController = FindObjectOfType<TargetController>();
            
            if (targetController == null)
                Debug.LogError("场景中未找到 TargetController！");
            else
                Debug.Log("已自动找到 TargetController");
        }

        if (collision.gameObject.CompareTag("Target"))
        {
            Debug.Log("击中靶盘！位置: " + collision.contacts[0].point);
            
            hasScored = true;
            int score = targetController.CalculateScore(collision.contacts[0].point);
            Debug.Log("计算得分: " + score);
            
            onScoreCalculated?.Invoke(score);
            
            // 固定飞镖位置
            Rigidbody rb = GetComponent<Rigidbody>();
            rb.isKinematic = true;
            transform.SetParent(collision.transform);
        }
        else
        {
            Debug.Log("击中其他物体，得0分");
            hasScored = true;
            onScoreCalculated?.Invoke(0);
        }
    }
}